package com.ykdl.member.kid.adapters;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.MyReanswerListAdapter.ViewHolder;
import com.ykdl.member.kid.beans.OfficialMessageBean;
import com.ykdl.member.kid.beans.SystemMessageBean;
import com.ykdl.member.kid.beans.SystemMessageExtraContextBean;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.util.DateUtil;
import com.ykdl.member.kid.widget.AsyncImageView;

import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class OfficialMessageAdapter1  extends BaseAdapter{
	private Activity mContext ;
	private ArrayList<SystemMessageExtraContextBean>  list;
	public void setList(ArrayList<SystemMessageExtraContextBean> list) {
		this.list = list;
	}

	public ArrayList<SystemMessageExtraContextBean> getList() {
		return list;
	}
	private BitmapUtils bitmapUtil ;
	public Map<Integer,Boolean> checkedMap;   //保存checkbox是否被选中的状态
	public Map<Integer,Integer> visibleMap;   //保存checkbox是否显示的状态
	public ArrayList<String> checked_message_id;  //该集合存储被选中的列表项中message_id
	public ArrayList<String> getChecked_message_id() {
		return checked_message_id;
	}
	public void clearList() {
		checked_message_id.clear();
	}
	public OfficialMessageAdapter1(Activity context  ){
		this.mContext = context;
		bitmapUtil = new BitmapUtils(mContext);
		checkedMap = new HashMap<Integer, Boolean>();
		visibleMap = new HashMap<Integer, Integer>();
		checked_message_id = new ArrayList<String>();
		
	}
	
	public void setlist(ArrayList<SystemMessageExtraContextBean>  list) {
		this.list = list ;
		for(int i=0;i<list.size();i++){
			checkedMap.put(i, false);
			visibleMap.put(i, CheckBox.GONE);
		}
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (list != null) {
			return list.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		if (getCount() != 0) {
			return list.get(position-1);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		 ViewHolder holder =  null ;
		final int index = position ;
		if (convertView==null) {
			convertView = RelativeLayout.inflate(mContext,
					R.layout.officialmessageadapter1_item, null);
			holder = new ViewHolder();
			 holder.user_name=  (TextView) convertView.findViewById(R.id.user_name);
			 holder.user_content=  (TextView) convertView.findViewById(R.id.user_content);
			 holder.user_time=  (TextView) convertView.findViewById(R.id.user_time);
			 holder.ischeck=  (CheckBox) convertView.findViewById(R.id.ischeck);
//			 holder.ischeck.setChecked(ischeck.get(position));
			
			
			 convertView.setTag(holder);
		}else {
			holder = (ViewHolder)convertView.getTag();
		}
		holder.ischeck.setVisibility(visibleMap.get(position));
		holder.ischeck.setChecked(checkedMap.get(position));
		if (checkedMap.get(position)) {
			if (!checked_message_id.contains(list.get(index).getMessage_id()+"")) {
				checked_message_id.add(list.get(index).getMessage_id()+"");
			}
		}else {
			checked_message_id.remove(list.get(index).getMessage_id()+"");
		}
		holder.user_name.setText(list.get(position).getTitle());
		holder.user_content.setText(list.get(position).getContext());
		holder.user_time.setText(DateUtil.getMessageDate(list.get(position)
				.getSend_time()));
		holder.ischeck.setOnClickListener(new View.OnClickListener() {  
            
            @Override  
            public void onClick(View v) {
            	if (!checked_message_id.contains(list.get(index).getMessage_id()+"")) {
					checked_message_id.add(list.get(index).getMessage_id()+"");
					checkedMap.put(index, true);
				}else {
					checkedMap.put(index, false);
					checked_message_id.remove(list.get(index).getMessage_id()+"");
				}
            	String delt_idString = getChecked_message_id().toString().replace("[", "").replace("]", "");
            	Log.e("alan", "--2----CheckedChange:"+delt_idString);
            }  
        });  
		return convertView;
	}
	class ViewHolder {
		 TextView  user_name,
		user_content,user_time;
		 CheckBox ischeck;
	}

}
